<template>
	<view>
		<view class="content">
			<view class="qingchu" @tap="uploadFile">
				<view class="text">头像</view>
				<image :src="user.http" mode="" class="qingchu2"></image>
				<image src="../../static/image/qianjin.png" mode="" class="qingchu1"></image>
			</view>
			<view class="qingchu" >
				<view class="text" style="flex: auto;" >昵称</view>
				<input type="text" class="text2" style="flex:1;text-align: right;"  v-model="user.username" />
			</view><strong></strong>
			<view class="qingchu" >
				<view class="text" style="">身份</view>
				<view class="text2" v-if="user.type==1">买家</view>
				<view class="text2" v-else >商家</view>
			</view>
			<!-- <view class="qingchu" style="border: unset;" @tap="show=true">
				<view class="text" style="">地区</view>
				<view class="text2"  >{{dizhi}}</view>
			</view> -->
		</view>
		
		<view class="content" style="background-color: #FFFFFF;">
			<view class=""style="padding-bottom: 40rpx;padding-right: 30rpx;">
				<view class="text">个性签名</view>
				<view class="" style="padding: 0 30rpx;">
					<!-- <textarea v-model="user.qianming" placeholder="" class="textareac" placeholder="请输入个性签名"/> -->
					<input type="text" class="text2" placeholder="请输入个性签名" style="width:100%;border: 0;background-color: #fff;margin-top: 0;margin-bottom: 10rpx;"  v-model="user.qianming" />
					<view class="" style="border-bottom: #09BB07 1rpx solid;"></view>
				</view>
				
			</view>
		</view>
		
		<view class="content-padding">
			<view class="denglv" @tap="submit()">
				保存
			</view>
		</view>
		<u-picker mode="region" v-model="show" :default-region='["江苏省", "南京市", "鼓楼区"]' @confirm="dizhiChang"></u-picker>
		<selfPopupMessage ref="selfPopupMessage"></selfPopupMessage>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user:{
					username:"",
					head:"",
					attestation:1,
					id:"",
					tuanduiid:"",
					type:2,
					qianming:"",
					http:"../../static/image/touxaing.png",
					
				},
				show:false,
				dizhi:"",
				shengshiqu:[]
			}
		},
		onReady() {
			var userinfo = uni.getStorageSync("userInfo");
			this.shengshiqu[0] =  userinfo.sheng;
			this.shengshiqu[1] =  userinfo.shi;
			this.shengshiqu[2] =  userinfo.qu;
			userinfo.http = userinfo.http;
			userinfo.userId = userinfo.id;
			this.user = userinfo;
			if(this.user.sheng !=undefined &&this.user.shi!=undefined && this.user.qu ){
				this.dizhi = this.user.sheng+this.user.shi +this.user.qu;
			}
			console.log(this.user);
			var that = this;
			// this.tim.on(that.$TIM.EVENT.SDK_NOT_READY, function(){
			
				// let timtoken = uni.getStorageSync("timToken");
				// let id = uni.getStorageSync("token");
				// console.log("登陆token："+timtoken);
				// let promise = that.tim.login({userID: id, userSig: timtoken});
				// promise.then(function(imResponse) {
				//   console.log(imResponse.data); // 登录成功
				//   if (imResponse.data.repeatLogin === true) {
				//     // 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
				//     console.log(imResponse.data.errorInfo);
				//   }
				// }).catch(function(imError) {
				// 	console.log(JSON.stringify(imError));
				// 	if(imError.code==70001){
				// 		/**获取聊天token*/
				// 		that.$util.nuiAjax(that.$jiekou.getTimToken, {
				// 			userid: id
				// 		}, function(res) {
				// 			if (res.result == "true") {
				// 				uni.setStorageSync("timToken", res.data);
				// 				that.timLogin();
				// 			}
				// 		})
				// 	}else{
				// 		 console.warn('login error:', imError); // 登录失败的相关信息
				// 	}
				 
				// });
			// });
		},
		onLoad() {
			// var userinfo = uni.getStorageSync("userInfo");
			// userinfo.http = userinfo.http;
			// userinfo.userId = userinfo.id;
			// this.user = userinfo;
		},
		methods: {
			dizhiChang(value){//地址选择
				this.user.sheng = value.province.label;
				this.user.shi = value.city.label;
				this.user.qu = value.area.label;
				this.dizhi = this.user.sheng+this.user.shi +this.user.qu;
			},
			uploadFile(){
				var that = this;
				uni.chooseImage({
				    success: (chooseImageRes) => {
				        var tempFilePaths = chooseImageRes.tempFilePaths;
						console.log(tempFilePaths);
				        uni.uploadFile({
				            url: that.$jiekou.avatarUpload, //仅为示例，非真实的接口地址
				            filePath: tempFilePaths[0],
				            name: 'file',
				            success: (res) => {
				                var data = JSON.parse(res.data);
								if(data.result=="200"){
									that.user.head=data.database;
									that.user.http=data.http;
									// console.log(JSON.stringify(that.user));
								}
				            }
				        });
				    },
					fail:function(){
						console.log("shibai");
					}
				});
			},
			submit(){
				var that = this;
				var id = uni.getStorageSync("token");
				this.$util.nuiAjax(this.$jiekou.updateUser,this.user,function(res){
					if(res.result=="true"){
						that.$refs.selfPopupMessage.open("保存成功");
						uni.setStorageSync("userInfo",that.user);
						uni.$emit("update_user");
						if( that.user.qianming==undefined) that.user.qianming ="";
						// 修改个人标配资料（即时聊天）
						let promise = that.tim.updateMyProfile({
						  nick: that.user.username,
						  avatar: that.user.head,
						  gender: that.$TIM.TYPES.GENDER_MALE,
						  selfSignature: that.user.qianming,
						  allowType: that.$TIM.TYPES.ALLOW_TYPE_ALLOW_ANY
						});
						promise.then(function(imResponse) {
						  console.log(imResponse.data); // 更新资料成功
						}).catch(function(imError) {
						  console.warn('updateMyProfile error:', imError); // 更新资料失败的相关信息
						});
						
						uni.navigateBack();
					}
				})	
			}
		}
	}
</script>

<style lang="scss">
	.textareac{
		height:200rpx;
		background-color: #F7F8F9;
		padding: 30rpx;
		border-radius:8rpx;
		font-size:28rpx;
		font-weight:400;
		color:rgba(138,139,144,1);
		line-height:35rpx;
	}
	
	.content-padding{
		padding: 0 54upx 0 54upx;
	}
	.denglv{
		height:96rpx;
		background: $uni-color-primary;
		border-radius:48rpx;
		margin-top: 80rpx;
		line-height: 96rpx;
		text-align: center;
		font-size: 32rpx;
		color: $uni-text-color-inverse;
		letter-spacing: 1rpx;
	}
	page{
		background-color:$page-bg-color;
	}
	.text2{
		// height:40rpx;
		font-size:28rpx;
		font-weight:400;
		color:rgba(138,139,144,1);
		line-height:40rpx;
		margin-right: 62rpx;
		margin-top: 30rpx;
	}
	.qingchu2{
		width: 70rpx;
		height: 70rpx;
		margin-top: 16rpx;
		// margin-bottom: 16rpx;
		margin-right: 22rpx;
		border-radius: 34rpx;
	}
	.content{
		background-color: #FFFFFF;
		margin-top: 20rpx;
		padding-left: 30rpx;
		box-shadow:0px 8rpx 16rpx 0px rgba(246,247,248,1);
	}
	.qingchu{
		display: flex;
		flex-flow: row;
		justify-content: space-between;
		border-bottom: 1px solid #EFEFEF;
	}
	.huncun{
		flex:1;
		text-align: right;
		margin-right: 20rpx;
	}
	.qingchu1{
		margin-right: 30rpx;
		width: 10rpx;
		height: 20rpx;
		margin-top: 38rpx;
	}
	.text{
		// height:40rpx;
		font-size:28rpx;
		font-weight:400;
		line-height:40rpx;
		padding-top: 30rpx;
		flex: 1;
		padding-bottom: 32rpx;
	}
</style>
